
import { BaseRequest } from "@hw-agconnect/credential-ohos";

export class FunctionLocalRequest extends BaseRequest {
  private requestUrl: string;
  private readonly body: any;

  constructor(name: string, httpTriggerURI: string, body: any, url: string) {
    super(name);
    this.setSdkServiceName("agconnect-function-local");
    this.body = body;
    // 兼容本地url 和 triggerUrl
    let finalUrl = "";
    if (url.charAt(url.length - 1) == "/") {
      finalUrl = url;
    }else {
      finalUrl = url + "/";
    }
    let finalHttpTriggerURI = "";
    let searchStr = "-$"
    if (httpTriggerURI.length > 0 && httpTriggerURI.indexOf(searchStr) != -1) {
      finalHttpTriggerURI = httpTriggerURI.substring(0,httpTriggerURI.indexOf(searchStr));
    } else {
      finalHttpTriggerURI = httpTriggerURI;
    }
    this.requestUrl = finalUrl + finalHttpTriggerURI + "/invoke"
  }

  async getBody(): Promise<any> {
    return Promise.resolve(this.body);
  }

  async getHeader(): Promise<any> {
    let header = await super.getHeader();
    return Promise.resolve(header);
  }

  async getUrl(): Promise<string> {
    return Promise.resolve(this.requestUrl);
  }
}